package com.yayan.xh.controller;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.yayan.util.FilePathUtil;
import com.yayan.util.TenXunYunUtil;
import com.yayan.util.UploadUtil;
import com.yayan.xh.bean.BannerBean;
import com.yayan.xh.bean.EnterBean;
import com.yayan.xh.bean.EnterDetailBean;
import com.yayan.xh.bean.GradeBean;
import com.yayan.xh.bean.ImageBean;
import com.yayan.xh.bean.IntegralBean;
import com.yayan.xh.bean.MatchBean;
import com.yayan.xh.bean.NewsBean;
import com.yayan.xh.bean.OperateBean;
import com.yayan.xh.bean.ReviewBean;
import com.yayan.xh.service.IBannerService;
import com.yayan.xh.service.IEnterDetailService;
import com.yayan.xh.service.IEnterService;
import com.yayan.xh.service.IGradeService;
import com.yayan.xh.service.IImageService;
import com.yayan.xh.service.IIntegralService;
import com.yayan.xh.service.IMatchService;
import com.yayan.xh.service.INewsService;
import com.yayan.xh.service.IOperateService;
import com.yayan.xh.service.IOrderService;
import com.yayan.xh.service.IReviewService;

@Controller
@RequestMapping(value="/back")

public class BackController {
	
	@Autowired
	private IMatchService matchService;
	
	@Autowired
	private IOrderService orderService;
	
	
	@Autowired
	private IReviewService reviewService;
	
	@Autowired
	private IEnterService enterService;
	
	@Autowired
	private IIntegralService integralService;
	
	@Autowired
	private INewsService newsService;
	
	@Autowired
	private IOperateService operateService;
	
	@Autowired
	private IImageService imageService;
	
	@Autowired
	private IGradeService gradeService;
	
	@Autowired
	private IBannerService bannerService;
	
	@Autowired
	private IEnterDetailService enterDetailService;
	
	/**
	 * 预支付回调
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getBackOrder.do")
	@ResponseBody
	public String getBackOrder(@RequestBody String xml) {
		String result = null;
		//回调处理并保存报名信息
		result = orderService.backOrder(xml);
		return result;
	}
	
	/**
	 * 后台登录
	 * @param userName
	 * @param password
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/adminLogin.do", method=RequestMethod.POST)
	public String adminLogin(@RequestParam Map<String, String> map){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean result = operateService.adminLogin(map);
		if(null == result){			
			request.setAttribute("username", map.get("username"));
			request.setAttribute("password", map.get("password"));
			request.setAttribute("errorMessage", "账号或密码错误！");
			return "login";
		}
		request.getSession().setAttribute("admin",result);
		return "redirect:/back/getMatch.do";
	}
	
	/**
	 * 后台注销
	 * @return
	 */
	@RequestMapping(value="/adminLogout.do")
	public String adminLogout() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		request.getSession().removeAttribute("admin");
		return "login";
	}
	
	/**
	 * 查询赛事信息
	 * @return
	 */
	@RequestMapping(value="/getMatch.do")
	public String getMatch(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<MatchBean> list = matchService.getMatchListBack(map);
		request.setAttribute("list", list);
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		return "match";
	}
	
	/**
	 * 查询赛事图片信息
	 * @return
	 */
	@RequestMapping(value="/getMatchImg.do")
	public String getMatchImg(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<ImageBean> list = imageService.getImageByMatchIdBack(map);
		request.setAttribute("list", list);
		request.setAttribute("matchId", map.get("matchId"));
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		return "modifyimage";
	}
	
	/**
	 * 录入赛事图片信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveImg.do", produces = "text/plain;charset=UTF-8")
	public String saveImg(MultipartFile image, @RequestParam Map<String, String> map) throws Exception{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(image != null && !image.isEmpty()){
			if(image.getSize() >= 1048576 || !UploadUtil.checkExtention(image)) {
				request.setAttribute("message", "图片必须小于1M，且格式必须为jpg/jpeg/png");
				return "redirect:/back/getMatchImg.do?matchId=" + map.get("matchId");
			}
			// 获取图片原始文件名
			String originalFilename = image.getOriginalFilename();
			// 文件名使用当前时间
			String currentName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String videourl = FilePathUtil.getFilePath();			
			File dir = new File(videourl);
			if(!dir.exists()) {
				dir.mkdirs();
			}
			currentName += "_img_." + FilenameUtils.getExtension(originalFilename);
			//本地上传
			image.transferTo(new File(videourl+currentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(currentName, 0, videourl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
	        
			//本地删除
	        dir = new File(videourl + currentName.substring(currentName.lastIndexOf("/")+1, currentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			
			map.put("image", stb.toString());
			map.put("shopId", admin.getShop_id());
			if(imageService.saveImageBack(map)) {
				request.getSession().setAttribute("message", "保存成功");
			} else {
				request.getSession().setAttribute("message", "保存失败");
			}
				
		}
		return "redirect:/back/getMatchImg.do?matchId=" + map.get("matchId");
	}
	
	/**
	 * 删除图片信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/delImg.do")
	public String delImg(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		ImageBean bean = imageService.getImageByIdBack(map);
		
		if(imageService.delImageBack(map)) {
			request.getSession().setAttribute("message", "删除成功");
			
			//云删除
			if(null != bean.getIm_url()) 
				TenXunYunUtil.delFile(bean.getIm_url());
		} else {
			request.getSession().setAttribute("message", "删除失败");
		}
		
		return "redirect:/back/getMatchImg.do?matchId=" + map.get("matchId");
	}
	
	/**
	 * 添加赛事信息跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addMatch.do")
	public String addMatch(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		return "savematch";
	}
	
	/**
	 * 录入赛事信息
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/saveMatch.do", method=RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public String saveMatch(@RequestParam Map<String, String> map) throws UnsupportedEncodingException{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("name", new String(map.get("name").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("address", new String(map.get("address").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("detail", new String(map.get("detail").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("text", new String(map.get("text").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("shopId", admin.getShop_id());
		if(matchService.saveMatch(map)) {
			request.getSession().setAttribute("message", "添加成功");
		} else {
			request.getSession().setAttribute("message", "添加失败");
		}
		return "redirect:/back/getMatch.do";
	}
	
	/**
	 * 查看报名信息
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/getEnter.do")
	public String getEnter(@RequestParam Map<String, String> map) {		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<EnterBean> list = enterService.getBackEnter(map);
		request.setAttribute("list", list);
		request.setAttribute("mId", map.get("matchId"));
		return "enter";
	}
	
	/**
	 * 修改赛事信息跳转
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/updateMatch.do")
	public String updateMatch(@RequestParam Map<String, String> map) throws UnsupportedEncodingException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		MatchBean bean = matchService.getMatchByMatchIdBack(map);
		request.setAttribute("bean", bean);
		return "modifymatch";
	}
	
	/**
	 * 修改赛事信息
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/modifyMatch.do")
	public String modifyMatch(@RequestParam Map<String, String> map) throws UnsupportedEncodingException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("detail", new String(map.get("detail").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("name", new String(map.get("name").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("address", new String(map.get("address").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("text", new String(map.get("text").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("shopId", admin.getShop_id());
		if(matchService.modifyMatch(map)) {
			request.getSession().setAttribute("message", "修改成功");
		} else {
			request.getSession().setAttribute("message", "修改失败");
		}
		return "redirect:/back/getMatch.do";
	}
	
	/**
	 * 赛事回顾跳转
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/updateReview.do")
	public String updateReview(@RequestParam Map<String, String> map) throws UnsupportedEncodingException{
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		ReviewBean bean = reviewService.getReviewByMatchId(map);
		request.setAttribute("bean", bean);
		request.setAttribute("matchId", map.get("matchId"));
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		return "modifyreview";
	}
	
	/**
	 * 录入回顾信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveReview.do", produces = "text/plain;charset=UTF-8")
	public String saveReview(MultipartFile videoadd, MultipartFile poster, @RequestParam Map<String, String> map) throws Exception{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(null != videoadd && !videoadd.isEmpty()) {
			if(!UploadUtil.checkExtentionVideo(videoadd)) {
				request.getSession().setAttribute("message", "视频格式必须为mp4");
				return "redirect:/back/updateReview.do?matchId=" + map.get("matchId");
			}
			//视频上传
			// 获取原始文件名
			String videoFile = videoadd.getOriginalFilename();
			// 文件名使用当前时间
			String videocurrentName= new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String videourl = FilePathUtil.getFilePath();			
			File videodir = new File(videourl);
			if(!videodir.exists()) {
				videodir.mkdirs();
			}
			videocurrentName += "_vedio_." + FilenameUtils.getExtension(videoFile);
			//本地上传
			videoadd.transferTo(new File(videourl+videocurrentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(videocurrentName, 2, videourl);
	        
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
			
			//本地删除
	        File dir = new File(videourl + videocurrentName.substring(videocurrentName.lastIndexOf("/")+1, videocurrentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			
			//云删除
			if(null != map.get("oldvideoadd") && !"".equals(map.get("oldvideoadd"))) 
				TenXunYunUtil.delFile(map.get("oldvideoadd"));
			
			map.put("videoadd", stb.toString());
		} else {
			map.put("videoadd", map.get("oldvideoadd"));
		}
		
		if(null != poster && !poster.isEmpty()) {
			if(!UploadUtil.checkExtention(poster)) {
				request.getSession().setAttribute("message", "图片必须小于1M，且格式必须为jpg/jpeg/png");
				return "redirect:/back/updateReview.do?matchId=" + map.get("matchId");
			}
			//图片上传
			// 获取原始文件名
			String imgFile = poster.getOriginalFilename();
			// 文件名使用当前时间
			String imgcurrentName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String imgurl = FilePathUtil.getFilePath();			
			File imgdir = new File(imgurl);
			if(!imgdir.exists()) {
				imgdir.mkdirs();
			}
			imgcurrentName += "_poster_." + FilenameUtils.getExtension(imgFile);
			//本地上传
			poster.transferTo(new File(imgurl+imgcurrentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(imgcurrentName, 1, imgurl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
	        
			//本地删除
	        File dir = new File(imgurl + imgcurrentName.substring(imgcurrentName.lastIndexOf("/")+1, imgcurrentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			
			//云删除
			if(null != map.get("oldposter") && !"".equals(map.get("oldposter"))) 
				TenXunYunUtil.delFile(map.get("oldposter"));
			
			map.put("poster", stb.toString());
		} else {
			map.put("poster", map.get("oldposter"));
		}
					
		map.put("shopId", admin.getShop_id());
		if(reviewService.saveReview(map)) {
			request.getSession().setAttribute("message", "保存成功");
		} else {
			request.getSession().setAttribute("message", "保存失败");
		}
		return "redirect:/back/updateReview.do?matchId=" + map.get("matchId");
	}
	
	/**
	 * 查询积分
	 * @return
	 */
	@RequestMapping(value="/getIntegral.do")
	public String getIntegral(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<IntegralBean> list = integralService.getBackIntegral(map);
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		request.setAttribute("list", list);
		return "integral";
	}
	
	/**
	 * 修改积分跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/updateIntegral.do")
	public String updateIntegral(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		IntegralBean bean = integralService.getBackIntegralByUserId(map);
		request.setAttribute("bean", bean);
		return "modifyintegral";
	}
	
	/**
	 * 修改积分
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/modifyIntegral.do")
	public String modifyIntegral(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		if(integralService.modifyIntegral(map)) {
			request.getSession().setAttribute("message", "修改成功");
		} else {
			request.getSession().setAttribute("message", "修改失败");
		}
		
		return "redirect:/back/getIntegral.do";
	}
	
	/**
	 * 查询等级
	 * @return
	 */
	@RequestMapping(value="/getGrade.do")
	public String getGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<GradeBean> list = gradeService.getGradeList(map);
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		request.setAttribute("list", list);
		return "grade";
	}
	
	/**
	 * 添加等级跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addGrade.do")
	public String addGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		return "savegrade";
	}
	
	/**
	 * 添加等级
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveGrade.do")
	public String saveGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		if(gradeService.saveGrade(map)) {
			request.getSession().setAttribute("message", "添加成功");
		} else {
			request.getSession().setAttribute("message", "添加失败");
		}
		return "redirect:/back/getGrade.do";
	}
	
	
	/**
	 * 修改等级跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/updateGrade.do")
	public String updateGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		GradeBean bean = gradeService.getGradeById(map);
		request.setAttribute("bean", bean);
		return "modifygrade";
	}
	
	/**
	 * 修改等级
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/modifyGrade.do")
	public String modifyGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		if(gradeService.modifyGrade(map)) {
			request.getSession().setAttribute("message", "修改成功");
		} else {
			request.getSession().setAttribute("message", "修改失败");
		}
		return "redirect:/back/getGrade.do";
	}
	
	/**
	 * 删除等级
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/delGrade.do")
	public String delGrade(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		if(gradeService.delGrade(map)) {
			request.getSession().setAttribute("message", "删除成功");
		} else {
			request.getSession().setAttribute("message", "删除失败");
		}
		return "redirect:/back/getGrade.do";
	}
	
	/**
	 * 查询新闻资讯
	 * @return
	 */
	@RequestMapping(value="/getNews.do")
	public String getNews(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		List<NewsBean> list = newsService.getNewsBack(map); 
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		request.setAttribute("list", list);
		return "news";
	}
	
	/**
	 * 添加新闻资讯跳转
	 * @return
	 */
	@RequestMapping(value="/addNews.do")
	public String addNews(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		return "savenews";
	}
	
	/**
	 * 录入新闻资讯信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/saveNews.do", produces = "text/plain;charset=UTF-8")
	public String saveNews(MultipartFile videoadd, @RequestParam Map<String, String> map) throws Exception{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(null != videoadd && !videoadd.isEmpty() && videoadd.getSize() > 0) {
			if(!UploadUtil.checkExtentionVideo(videoadd)) {
				request.getSession().setAttribute("message", "视频格式必须为mp4");
				return "redirect:/back/getNews.do";
			}
			//视频上传
			// 获取原始文件名
			String videoFile = videoadd.getOriginalFilename();
			// 文件名使用当前时间
			String videocurrentName= new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String videourl = FilePathUtil.getFilePath();			
			File videodir = new File(videourl);
			if(!videodir.exists()) {
				videodir.mkdirs();
			}
			videocurrentName += "_vedio_." + FilenameUtils.getExtension(videoFile);
			//本地上传
			videoadd.transferTo(new File(videourl+videocurrentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(videocurrentName, 3, videourl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
	        
			//本地删除
	        File dir = new File(videourl + videocurrentName.substring(videocurrentName.lastIndexOf("/")+1, videocurrentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			
			
			map.put("videoadd", stb.toString());
		} else {
			map.put("videoadd", null);
		}
		
		map.put("shopId", admin.getShop_id());
		if(newsService.saveNews(map)) {
			request.getSession().setAttribute("message", "保存成功");
		} else {
			request.getSession().setAttribute("message", "保存失败");
		}
					
		return "redirect:/back/getNews.do";
	}
	
	/**
	 * 修改资讯跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/updateNews.do")
	public String updateNews(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		NewsBean bean = newsService.getNewsByIdBack(map);
		request.setAttribute("bean", bean);
		return "modifynews";
	}
	
	/**
	 * 修改新闻资讯信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/modifyNews.do", produces = "text/plain;charset=UTF-8")
	public String modifyNews(MultipartFile videoadd, @RequestParam Map<String, String> map) throws Exception{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(null != videoadd && !videoadd.isEmpty() && videoadd.getSize() > 0) {
			if(!UploadUtil.checkExtentionVideo(videoadd)) {
				request.getSession().setAttribute("message", "视频格式必须为mp4");
				return "redirect:/back/getNews.do";
			}
		
			//视频上传
			// 获取原始文件名
			String videoFile = videoadd.getOriginalFilename();
			// 文件名使用当前时间
			String videocurrentName= new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String videourl = FilePathUtil.getFilePath();			
			File videodir = new File(videourl);
			if(!videodir.exists()) {
				videodir.mkdirs();
			}
			videocurrentName += "_vedio_." + FilenameUtils.getExtension(videoFile);
			//本地上传
			videoadd.transferTo(new File(videourl+videocurrentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(videocurrentName, 3, videourl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
			
			//本地删除
	        File dir = new File(videourl + videocurrentName.substring(videocurrentName.lastIndexOf("/")+1, videocurrentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			if(null != map.get("oldvideoadd") && map.get("oldvideoadd").length() > 0) 
			//云删除
				TenXunYunUtil.delFile(map.get("oldvideoadd"));
			map.put("videoadd", stb.toString());
		} else {
			map.put("videoadd", map.get("oldvideoadd"));
		}
		
		map.put("shopId", admin.getShop_id());
		if(newsService.modifyNews(map)) {
			request.getSession().setAttribute("message", "修改成功");
			
		} else {
			request.getSession().setAttribute("message", "修改失败");
		}
					
		return "redirect:/back/getNews.do";
	}
	
	
	/**
	 * 修改新闻资讯信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/delnews.do", produces = "text/plain;charset=UTF-8")
	public String delnews(@RequestParam Map<String, String> map) throws Exception{		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		//获取新闻信息
		NewsBean newsBean = newsService.getNewsByIdBack(map);
		if(null != newsBean.getN_videoadd()) {
			//云删除
			TenXunYunUtil.delFile(newsBean.getN_videoadd());
		}
		
		if(newsService.delNews(map)) {
			request.getSession().setAttribute("message", "删除成功");
		} else {
			request.getSession().setAttribute("message", "删除失败");
		}
					
		return "redirect:/back/getNews.do";
	}
	/**
	 * 抽奖跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getLuckDraw.do")
	public String getLuckDraw(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		//获得抽奖名单
		List<EnterBean> list = enterService.getLuckDraw(map);
		request.setAttribute("list", list);
		//获得中奖名单
		List<EnterBean> luckBoys = enterService.getLuckBoys(map);
		request.setAttribute("luckBoys", luckBoys);
		return "luckdraw";
	}
	
	/**
	 * 中奖处理
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/luckyBoy.do")
	public String luckyBoy(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		enterService.updateLuckBoy(map);
		return null;
	}
	
	/**
	 * 广告查询
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/getBanner.do")
	public String getBanner(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		List<BannerBean> list = bannerService.getBanner(map);
		request.setAttribute("list", list);
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		return "banner";
	}
	
	/**
	 * 广告录入跳转
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addBanner.do")
	public String addBanner(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		
		return "savebanner";
	}
	
	/**
	 * 广告录入
	 * @param map
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value="/saveBanner.do")
	public String saveBanner(MultipartFile image, @RequestParam Map<String, String> map) throws IllegalStateException, IOException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(image != null && !image.isEmpty()){
			if(image.getSize() >= 1048576 || !UploadUtil.checkExtention(image)) {
				request.setAttribute("message", "图片必须小于1M，且格式必须为jpg/jpeg/png");
				return "redirect:/back/getBanner.do";
			}
			// 获取图片原始文件名
			String originalFilename = image.getOriginalFilename();
			// 文件名使用当前时间
			String currentName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String imgurl = FilePathUtil.getFilePath();			
			currentName += "_banner_." + FilenameUtils.getExtension(originalFilename);
			
			File dir = new File(imgurl);
			if(!dir.exists()) {
				dir.mkdirs();
			}
			//本地上传
			image.transferTo(new File(imgurl+currentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(currentName, 4, imgurl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
			
			//本地删除
	        dir = new File(imgurl + currentName.substring(currentName.lastIndexOf("/")+1, currentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
	        
			map.put("img", stb.toString());
			map.put("shopId", admin.getShop_id());
			if("1".equals(map.get("type")) || "2".equals(map.get("type"))) {
				map.put("matchId", map.get("link"));
				MatchBean matchBean = matchService.getMatchByMatchIdBack(map);
				map.put("linkName", matchBean.getM_name());
			}
			if("3".equals(map.get("type"))) {
				map.put("newsId", map.get("link"));
				NewsBean newsBean = newsService.getNewsByIdBack(map);
				map.put("linkName", newsBean.getN_title());
			}
			if(bannerService.saveBanner(map)) {
				request.getSession().setAttribute("message", "保存成功");
			} else {
				request.getSession().setAttribute("message", "保存失败");
			}
				
		}
		
		return "redirect:/back/getBanner.do";
	}
	
	/**
	 * 广告修改跳转
	 * @param map
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value="/updateBanner.do")
	public String updateBanner(@RequestParam Map<String, String> map) throws IllegalStateException, IOException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		
		BannerBean bannerBean = bannerService.getBannerById(map);
		request.setAttribute("bannerBean", bannerBean);
		
		return "modifybanner";
	}
	
	/**
	 * 广告修改
	 * @param map
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value="/modifyBanner.do")
	public String modifyBanner(MultipartFile image, @RequestParam Map<String, String> map) throws IllegalStateException, IOException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		if(null != image && !image.isEmpty()) {
			if(image.getSize() >= 1048576 || !UploadUtil.checkExtention(image)) {
				request.setAttribute("message", "图片必须小于1M，且格式必须为jpg/jpeg/png");
				return "redirect:/back/getBanner.do";
			}
			//图片上传
			// 获取原始文件名
			String imgFile = image.getOriginalFilename();
			// 文件名使用当前时间
			String imgcurrentName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
			String imgurl = FilePathUtil.getFilePath();			
			File imgdir = new File(imgurl);
			if(!imgdir.exists()) {
				imgdir.mkdirs();
			}
			imgcurrentName += "_banner_." + FilenameUtils.getExtension(imgFile);
			//本地上传
			image.transferTo(new File(imgurl+imgcurrentName));
			
			//云上传
			String url = TenXunYunUtil.upFile(imgcurrentName, 4, imgurl);
			
			StringBuilder stb = new StringBuilder(url);
			stb.insert(4, "s");
			
			//本地删除
	        File dir = new File(imgurl + imgcurrentName.substring(imgcurrentName.lastIndexOf("/")+1, imgcurrentName.length()));
			if(dir.isFile() && dir.exists()) {
				dir.delete();
			}
			
			//云删除
			TenXunYunUtil.delFile(map.get("oldImg"));
			map.put("img", stb.toString());
		} else {
			map.put("img", map.get("oldImg"));
		}
		map.put("shopId", admin.getShop_id());
		
		if("1".equals(map.get("type")) || "2".equals(map.get("type"))) {
			map.put("matchId", map.get("link"));
			MatchBean matchBean = matchService.getMatchByMatchIdBack(map);
			map.put("linkName", matchBean.getM_name());
		}
		if("3".equals(map.get("type"))) {
			map.put("newsId", map.get("link"));
			NewsBean newsBean = newsService.getNewsByIdBack(map);
			map.put("linkName", newsBean.getN_title());
		}
		if(bannerService.modifyBanner(map)) {
			request.getSession().setAttribute("message", "修改成功");
		} else {
			request.getSession().setAttribute("message", "修改失败");
		}
		
		return "redirect:/back/getBanner.do";
	}
	
	/**
	 * 广告删除
	 * @param map
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value="/delBanner.do")
	public String delBanner(@RequestParam Map<String, String> map) throws IllegalStateException, IOException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		
		map.put("shopId", admin.getShop_id());
		
		if(bannerService.delBanner(map)) {
			request.getSession().setAttribute("message", "删除成功");
		} else {
			request.getSession().setAttribute("message", "删除失败");
		}
		
		return "redirect:/back/getBanner.do";
	}
	
	/**
	 * 下拉框联动
	 * @param map
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/changeType.do")
	public Map<String, Object> changeType(@RequestParam Map<String, String> map, HttpServletResponse response) throws IOException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		map.put("shopId", admin.getShop_id());
		JSONArray json = new JSONArray();
		List list = null;
		if("1".equals(map.get("type"))) {
			list = matchService.getMatchListBack(map);
			for (int i = 0; i < list.size(); i++) {
				MatchBean bean = (MatchBean) list.get(i);
				JSONObject jo = new JSONObject();
				jo.put("id", bean.getM_id()+"");
				jo.put("title", bean.getM_name()+"");
				json.put(jo);
			}
		} else if ("2".equals(map.get("type"))) {
			list = reviewService.getReviewBack(map);
			for (int i = 0; i < list.size(); i++) {
				ReviewBean bean = (ReviewBean) list.get(i);
				JSONObject jo = new JSONObject();
				jo.put("id", bean.getR_id()+"");
				jo.put("title", bean.getM_name()+"");
				json.put(jo);
			}
		} else if ("3".equals(map.get("type"))) {
			list = newsService.getNewsBack(map);
			for (int i = 0; i < list.size(); i++) {
				NewsBean bean = (NewsBean) list.get(i);
				JSONObject jo = new JSONObject();
				jo.put("id", bean.getN_id()+"");
				jo.put("title", bean.getN_title()+"");
				json.put(jo);
			}
		}
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
	    pw.print(json);
	    pw.flush();
	    pw.close();
	    return null;
	}
	
	/**
	 * 查询参赛者详情
	 * @param map
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/getEnterDetail.do")
	public String getEnterDetail(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		
		request.setAttribute("message", request.getSession().getAttribute("message"));
		request.getSession().removeAttribute("message");
		
		List<EnterDetailBean> list = enterDetailService.getEnterDetailByMatchId(map);
		request.setAttribute("list", list);
		request.setAttribute("mId", map.get("matchId"));
		return "enterdetail";
	}
	
	/**
	 * 添加参赛者详情跳转
	 * @return
	 */
	@RequestMapping(value="/addEnterDetail.do")
	public String addEnterDetail(@RequestParam Map<String, String> map) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		request.setAttribute("mId", map.get("matchId"));
		return "saveenterdetail";
	}
	
	/**
	 * 添加参赛者详情
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/saveEnterDetail.do", produces = "text/plain;charset=UTF-8")
	public String saveEnterDetail(@RequestParam Map<String, String> map) throws UnsupportedEncodingException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		
		map.put("name", new String(map.get("name").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("address", new String(map.get("address").getBytes("ISO-8859-1"), "UTF-8"));
		map.put("year", new String(map.get("year").getBytes("ISO-8859-1"), "UTF-8"));
		
		if(enterDetailService.saveEnterDetailFront(map)) {
			request.getSession().setAttribute("message", "添加成功！");
		} else {
			request.getSession().setAttribute("message", "添加失败！");
		}
		
		return "redirect:/back/getEnterDetail.do?matchId=" + map.get("matchId");
	}
	
	/**
	 * 删除参赛者详情
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/delEnterDetail.do")
	public String delEnterDetail(@RequestParam Map<String, String> map) throws UnsupportedEncodingException {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		OperateBean admin = (OperateBean) request.getSession().getAttribute("admin");
		if(null == admin) {
			request.setAttribute("errorMessage", "登录超时，请重新登录");
			return "login";
		}
		map.put("shopId", admin.getShop_id());
		
		if(enterDetailService.delEnterDetail(map)) {
			request.getSession().setAttribute("message", "删除成功！");
		} else {
			request.getSession().setAttribute("message", "删除失败！");
		}
		
		return "redirect:/back/getEnterDetail.do?matchId=" + map.get("matchId");
	}
}
